<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="姓名" prop="doctorName"
                :rules="[
                  {required: true, message: '姓名不能为空', trigger: 'blur'}
                 ]">
              <el-input v-model="inputForm.doctorName" placeholder="请填写姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="科室" prop="deptId"
                :rules="[
                  {required: true, message: '科室不能为空', trigger: 'change'}
                 ]">
          <SelectTree
                      ref="deptId"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

                      url="/sys/office/treeData?type=2"
                      :value="inputForm.deptId"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.deptId=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号" prop="idCardNumber"
                        :rules="[
                  {validator: validator.isCardId, trigger:'blur',required:true}
                 ]">
            <el-input v-model="inputForm.idCardNumber" placeholder="请填写身份证号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="gender"
                        :rules="[{required: true, message: '性别不能为空', trigger: 'blur'}]">
            <el-select v-model="inputForm.gender" placeholder="请选择" style="width: 100%;">
              <el-option
                v-for="item in $dictUtils.getDictList('sex')"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄" prop="age"
                        :rules="[
                  {validator: validator.isIntGteZero, trigger:'blur'}
                 ]">
            <el-input-number v-model="inputForm.age" placeholder="请填写年龄" style="width: 100%;"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="doctorPhone"
                        :rules="[
                  {validator: validator.isMobile, trigger:'blur',required:true}
                 ]">
            <el-input v-model="inputForm.doctorPhone" placeholder="请填写手机号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出生日期" prop="birthDate"
                        :rules="[
                 ]">
            <el-date-picker
              style="width: 100%;"
              v-model="inputForm.birthDate"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="住址" prop="address"
                        :rules="[
                 ]">
            <el-input v-model="inputForm.address" placeholder="请填写住址"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="医生介绍" prop="doctorDesc"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.doctorDesc" placeholder="请填写医生介绍"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import doctorInfoService from '@/api/doctor/doctorInfoService'
  import userService from '@/api/sys/userService'

  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          doctorName: '',
          deptId: '',
          doctorDesc: '',
          idCardNumber: '',
          gender: '',
          age: '',
          doctorPhone: '',
          birthDate: '',
          address: ''
        },
        userForm: {
          id: '',
          companyDTO: { // 归属医院
            id: '',
            name: ''
          },
          officeDTO: {
            id: '',
            name: ''
          },
          roleIdList: [],
          postIdList: [],
          loginName: '', // 登录名
          no: '', // 工号
          name: '', // 姓名
          email: '', // 邮箱
          phone: '', // 电话
          mobile: '', // 手机
          loginFlag: '1', // 是否允许登陆
          photo: '', // 头像
          qrCode: '', // 二维码
          oldLoginName: '', // 原登录名
          newPassword: '', // 新密码
          confirmNewPassword: '',
          sign: '', // 签名
          remarks: '', // 备注
          idCardNumber: '',
          gender: '',
          age: '',
          birthDate: '',
          address: ''
        }
      }
    },
    components: {
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建医生信息`
        } else if (method === 'edit') {
          this.title = '修改医生信息'
        } else if (method === 'view') {
          this.title = '查看医生信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            doctorInfoService.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            doctorInfoService.save(this.inputForm).then(({data}) => {
              this.visible = false
              this.$message.success('操作成功')
              this.$emit('refreshDataList')
              this.loading = false
              if (this.method === 'add') {
                this.saveUser(data.id)
              }
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      // 保存用户
      saveUser (id) {
        // 赋值操作
        this.userForm.id = id
        this.userForm.name = this.inputForm.doctorName
        this.userForm.idCardNumber = this.inputForm.idCardNumber
        this.userForm.mobile = this.inputForm.doctorPhone
        this.userForm.birthDate = this.inputForm.birthDate
        this.userForm.address = this.inputForm.address
        this.userForm.roleIdList = ['38a14f1f42ed424eab4cb5d489596b0d']
        this.userForm.loginName = this.inputForm.idCardNumber
        this.userForm.companyDTO = {id: '1504779078102695938', name: '某三甲医院'}
        this.userForm.officeDTO =
          {id: this.inputForm.deptId}
        this.userForm.no = 1
        this.userForm.newPassword = '123456'
        this.userForm.confirmNewPassword = '123456'
        userService.saveUser(this.userForm)
      }
    }
  }
</script>


